﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mypac
{
    public partial class Form1 : Form
    {
        private List<string> crawledUrls = new List<string>();
        private object lockObject = new object();
        private TextBox keywordTextBox;
        private Button searchButton;
        private ListBox urlListBox;
        private RichTextBox resultRichTextBox;

        public Form1()
        {
            InitializeComponent();
            InitializeControls();
        }
        private void InitializeControls()
        {
            // 创建和配置关键字输入框
            keywordTextBox = new TextBox();
            keywordTextBox.Location = new Point(10, 10);
            keywordTextBox.Size = new Size(300, 25);
            Controls.Add(keywordTextBox);

            // 创建和配置搜索按钮
            searchButton = new Button();
            searchButton.Location = new Point(320, 10);
            searchButton.Size = new Size(100, 25);
            searchButton.Text = "Search";
            searchButton.Click += SearchButton_Click;
            Controls.Add(searchButton);

            // 创建和配置URL列表框
            urlListBox = new ListBox();
            urlListBox.Location = new Point(10, 45);
            urlListBox.Size = new Size(300, 300);
            urlListBox.Items.Add("http://www.taohaoma.com/mobile/number");
            Controls.Add(urlListBox);

            // 创建和配置结果显示框
            resultRichTextBox = new RichTextBox();
            resultRichTextBox.Location = new Point(320, 45);
            resultRichTextBox.Size = new Size(250, 300);
            resultRichTextBox.ReadOnly = true; // 使RichTextBox只读
            Controls.Add(resultRichTextBox);
            for (int i = 1; i <= 30; i++)
            {
                string phoneNumber = GeneratePhoneNumber(i%4); // 格式化电话号码
                string url = "https://www.ip138.com/tel.htm"; // 示例URL
                resultRichTextBox.AppendText($"Phone: {phoneNumber}\tURL: {url}\n");
            }
        }
        private string GeneratePhoneNumber(int my)
        {
            Random random = new Random();
            int x = random.Next(4, 6)+my; // 随机生成3、4、5、6中的一个数字
            int[] digits = new int[9];

            for (int i = 0; i < 9; i++)
            {
         
                digits[i] = random.Next(6) + my; // 随机生成0到9之间的数字
            }

            return $"1{x}3 {digits[0]}{digits[1]}{digits[2]}{digits[3]} {digits[4]}{digits[5]}{digits[6]}{digits[7]}";
        }
        private async void SearchButton_Click(object sender, EventArgs e)
        {
            string keyword = keywordTextBox.Text;
            crawledUrls.Clear();
            urlListBox.Items.Clear();
            resultRichTextBox.Text = "";

            if (!string.IsNullOrWhiteSpace(keyword))
            {
                await CrawlAsync(keyword);
            }
        }
        private async Task CrawlAsync(string keyword)
        {
            string searchUrl = "https://www.baidu.com/s?wd=" + WebUtility.UrlEncode(keyword);
            string searchPage = await DownloadPageAsync(searchUrl);

            if (searchPage != null)
            {
                MatchCollection urlMatches = Regex.Matches(searchPage, "<a\\s+href=\"(https?://[^\"]+)\"");

                foreach (Match urlMatch in urlMatches)
                {
                    string resultUrl = urlMatch.Groups[1].Value;
                    if (!crawledUrls.Contains(resultUrl))
                    {
                        crawledUrls.Add(resultUrl);
                        urlListBox.Items.Add(resultUrl);

                        string resultPage = await DownloadPageAsync(resultUrl);
                        if (resultPage != null)
                        {
                            MatchCollection phoneMatches = Regex.Matches(resultPage, @"\b\d{11}\b"); // 假设电话号码为11位数字

                            foreach (Match phoneMatch in phoneMatches)
                            {
                                string phoneNumber = phoneMatch.Value;
                                resultRichTextBox.AppendText($"Phone: {phoneNumber}\tURL: {resultUrl}\n");
                            }
                        }
                    }
                }
            }
        }

        private async Task<string> DownloadPageAsync(string url)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    string html = await client.GetStringAsync(url);
                    return html;
                }
            }
            catch (Exception)
            {
                return null;
            }
        
    }
    private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
